// src/utils/permissionUtils.ts

import { useUserStore } from '@/store/userStore';

// Permission IDs mapping
export const PERMISSIONS = {
  SCHOLARS_DASHBOARD_VIEW: 1,
  SCHOLARS_DASHBOARD_EDIT: 2,
  SPONSORS_DASHBOARD_VIEW: 3,
  SPONSORS_DASHBOARD_EDIT: 4,
  SCHOLARS_REPORTS_VIEW: 5,
  SCHOLARS_REPORTS_EXPORT: 6,
  SPONSORS_REPORTS_VIEW: 7,
  SPONSORS_REPORTS_EXPORT: 8,
  SCHOLARS_DASHBOARD_NONE: 9,
  SPONSORS_DASHBOARD_NONE: 10,
  SCHOLARS_REPORTS_NONE: 11,
  SPONSORS_REPORTS_NONE: 12,
} as const;

// Permission checking functions
export const hasPermission = (userPermissions: number[], requiredPermission: number): boolean => {
  return userPermissions.includes(requiredPermission);
};

export const hasAnyPermission = (userPermissions: number[], requiredPermissions: number[]): boolean => {
  return requiredPermissions.some(permission => userPermissions.includes(permission));
};

export const hasAllPermissions = (userPermissions: number[], requiredPermissions: number[]): boolean => {
  return requiredPermissions.every(permission => userPermissions.includes(permission));
};

// Dashboard content access functions (for rendering main content)
export const canViewScholarsDashboardContent = (userPermissions: number[]): boolean => {
  // User can view content if they have VIEW or EDIT permission, but not NONE
  return hasAnyPermission(userPermissions, [
    PERMISSIONS.SCHOLARS_DASHBOARD_VIEW,
    PERMISSIONS.SCHOLARS_DASHBOARD_EDIT
  ]) && !hasPermission(userPermissions, PERMISSIONS.SCHOLARS_DASHBOARD_NONE);
};

export const canViewSponsorsDashboardContent = (userPermissions: number[]): boolean => {
  // User can view content if they have VIEW or EDIT permission, but not NONE
  return hasAnyPermission(userPermissions, [
    PERMISSIONS.SPONSORS_DASHBOARD_VIEW,
    PERMISSIONS.SPONSORS_DASHBOARD_EDIT
  ]) && !hasPermission(userPermissions, PERMISSIONS.SPONSORS_DASHBOARD_NONE);
};

export const canEditScholarsDashboard = (userPermissions: number[]): boolean => {
  return hasPermission(userPermissions, PERMISSIONS.SCHOLARS_DASHBOARD_EDIT);
};

export const canEditSponsorsDashboard = (userPermissions: number[]): boolean => {
  return hasPermission(userPermissions, PERMISSIONS.SPONSORS_DASHBOARD_EDIT);
};

// Reports access functions
export const canViewScholarsReports = (userPermissions: number[]): boolean => {
  return hasPermission(userPermissions, PERMISSIONS.SCHOLARS_REPORTS_VIEW) &&
    !hasPermission(userPermissions, PERMISSIONS.SCHOLARS_REPORTS_NONE);
};

export const canExportScholarsReports = (userPermissions: number[]): boolean => {
  return hasPermission(userPermissions, PERMISSIONS.SCHOLARS_REPORTS_EXPORT) &&
    !hasPermission(userPermissions, PERMISSIONS.SCHOLARS_REPORTS_NONE);
};

export const canViewSponsorsReports = (userPermissions: number[]): boolean => {
  return hasPermission(userPermissions, PERMISSIONS.SPONSORS_REPORTS_VIEW) &&
    !hasPermission(userPermissions, PERMISSIONS.SPONSORS_REPORTS_NONE);
};

export const canExportSponsorsReports = (userPermissions: number[]): boolean => {
  return hasPermission(userPermissions, PERMISSIONS.SPONSORS_REPORTS_EXPORT) &&
    !hasPermission(userPermissions, PERMISSIONS.SPONSORS_REPORTS_NONE);
};

// Check if user has any permissions at all
export const hasAnyDashboardOrReportPermission = (userPermissions: number[]): boolean => {
  return canViewScholarsDashboardContent(userPermissions) ||
    canViewSponsorsDashboardContent(userPermissions) ||
    canViewScholarsReports(userPermissions) ||
    canViewSponsorsReports(userPermissions) ||
    canExportScholarsReports(userPermissions) ||
    canExportSponsorsReports(userPermissions);
};

// Hook for easy permission checking
export const usePermissions = () => {
  const { user } = useUserStore();

  return {
    permissions: user.permissions,
    // Dashboard content access
    canViewScholarsDashboardContent: canViewScholarsDashboardContent(user.permissions),
    canViewSponsorsDashboardContent: canViewSponsorsDashboardContent(user.permissions),
    canEditScholarsDashboard: canEditScholarsDashboard(user.permissions),
    canEditSponsorsDashboard: canEditSponsorsDashboard(user.permissions),
    // Reports access
    canViewScholarsReports: canViewScholarsReports(user.permissions),
    canExportScholarsReports: canExportScholarsReports(user.permissions),
    canViewSponsorsReports: canViewSponsorsReports(user.permissions),
    canExportSponsorsReports: canExportSponsorsReports(user.permissions),
    // General permission checks
    hasAnyDashboardOrReportPermission: hasAnyDashboardOrReportPermission(user.permissions),
    hasPermission: (permission: number) => hasPermission(user.permissions, permission),
    hasAnyPermission: (permissions: number[]) => hasAnyPermission(user.permissions, permissions),
    hasAllPermissions: (permissions: number[]) => hasAllPermissions(user.permissions, permissions),
  };
}; 